<?php
# ============================================================================
# 版权所有  Chilcoo.com，并保留所有权利。
# 网站地址: http://www.chilcoo.com/erphp
# ----------------------------------------------------------------------------
# 这不是一个自由软件！你不能通过任何方式破解本软件.
# 使用:不允许对程序代码以任何形式任何目的的再发布。
# ============================================================================
# $Author: Chilcoo Inc. *
# $Official website : www.chilcoo.com
# 
# 文件信息:
# File : /bank/index.php
# 分类列表页面

# 载入常用集
# 导入common
require_once (dirname(__FILE__).'/../../include/common.inc.php');
require_once (dirname(__FILE__).'/../../include/_manager/login.inc.php');
# 页面级别
$conf_web_page_level = 0;

class page_index extends page{
	function __construct(){
		$this->database = new db(config::conf_db_host,config::conf_db_user,
			config::conf_db_password,config::conf_db_name);
	}
	function len(){
		$l = 0;
		try{
			$sql = "SELECT count(*) as cnt FROM cs2_bank WHERE (bankId > 2);";
			$row = $this->database->get_one($sql);
			$l = $row['cnt'];
		}catch (Exception $e) {
			$l = -1;
		}
		return $l;
	}
	function listview(){
		# important
		$lv;
		try{
			$sql = "SELECT bankId , bank, branch, bankno, name, url
				FROM cs2_bank WHERE (bankId > 2) ORDER BY bank DESC;";
			$lv = $this->database->select($sql);
		}catch (Exception $e) {
			$lv = null;
		}
		return $lv;
	}
	function ajax_callback(){
		# POST 数据
		$token = $_POST['token'];
		# 初始返回用 json
		$json = array("ref"=>"error");
		if($token == config::TOKEN_ADD_BANK){
			$bank = $_POST['bank'];
			$branch = $_POST['branch'];
			$bankno = $_POST['bankno'];
			$name = $_POST['name'];
			$url = $_POST['url'];
			$important = $_POST['important'];
			$sql = "INSERT INTO cs2_bank (bank, branch, bankno, name, url,important) 
			VALUES ('".$bank."','".$branch."','".$bankno."','".$name."','".$url."','".$important."');";
			try {
				$this->database->insert($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_DEL_BANK){
			$bankId = $_POST['bankId'];
			$sql = "DELETE FROM cs2_bank WHERE (bankId=".$bankId.")";
			try {
				$this->database->delete($sql);
				$json = array("ref"=>"OK","name");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_UP_BANK){
			$bank = $_POST['bank'];
			$branch = $_POST['branch'];
			$bankno = $_POST['bankno'];
			$name = $_POST['name'];
			$url = $_POST['url'];
			$important = $_POST['important'];
			$important_change = $_POST['important_change'];
			$bankId = $_POST['bankId'];
		
			$sql = "UPDATE cs2_bank SET 
				bank='".$bank."' ,
				branch='".$branch."' ,
				bankno='".$bankno."' ,
				name='".$name."' ,
				url='".$url."' ";
			if($important_change != -1)
				$sql = $sql." , important='".$important."' ";
			$sql = $sql." WHERE (bankId =". $bankId.");";
			try {
				$this->database->update($sql);
				$json = array("ref"=>"OK","name"=>$name,"bankId"=>$bankId);
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_LOAD_IMPORTANT){
			$bankId = $_POST['bankId'];
			$passwd = $_POST['important_passwd'];
			
			$sql = "select count(*) cnt FROM cs2_admin WHERE (passwd='".md5($passwd)."') AND (adminId=1) ";
			$row = $this->database->get_one($sql);
			if($row['cnt'] != 0){
				$sql = "select important FROM cs2_bank WHERE (bankId=".$bankId.") ";
				try {
					$row = $this->database->get_one($sql);
					$json = array("ref"=>"OK","important"=>$row['important']);
				} catch (Exception $e) {
					$json = array("ref"=>"error,database.");
				}
			}else{
				$json = array("ref"=>"NOPASS");
			}
		}
		return json_encode($json);
	}
}
$pg = new page_index();
# Session 和 POST 安全访问.
if($_SERVER['REQUEST_METHOD'] != 'POST'){
	$_SESSION[config::conf_session_id_token] = $config->web_page_period;
	$config->smarty->assign('len', $pg->len());
	$config->smarty->assign('listview', $pg->listview());
	# 加载页面
	$pg->render($config->temp_page_path.config::conf_web_template_extension);
}else{
	print_r($pg->ajax_callback());
}
